<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/23
 * Time: 11:48
 */

namespace app\index\controller;
use page\Page;
use think\Db;

class Gift
{
    /**
     * 礼包列表
     */
    public function index()
    {
        $where['start_time'] = ['<=', date('Y-m-d H:i:s', time())];
        $where['end_time'] = ['>=', date('Y-m-d H:i:s', time())];
        $where['status'] = ['eq', 1];
        $data = Db::name('gift g')
            ->fetchSql(false)
            ->where($where)
            ->join('gift_count gc', 'g.id = gc.gift_id')
            ->where('is_delete', 1)
            ->where('auding_status', 2)
            ->field('g.id,g.sycs,title,sale_price,origin_price,single_pic,DATE_FORMAT(start_time,"%Y.%m.%d") start_time,DATE_FORMAT(end_time,"%Y.%m.%d") end_time,sale_number,count')
            ->group('g.id')
            ->paginate(6)
            ->toArray();
        if (!empty($data)) {
            foreach ($data['data'] as $k => &$v) {
                $gift_count = Db::name('gift_count')->where('gift_id', $v['id'])->sum('gift_count');
                $v['gift_count'] = is_null($gift_count) ? 0 : $gift_count;
                $gift_service_count = Db::name('gift_count gc')
                    ->join('single s', 'gc.single_id = s.id', 'left')
                    ->where('gift_id', $v['id'])
                    ->field('cat_name,gift_count')
                    ->select();
                $gift_service_count_str = '';
                if (!empty($gift_service_count)) {
                    foreach ($gift_service_count as $k1 => $v1) {
                        $gift_service_count_str .= $v1['cat_name'] . ':' . $v1['gift_count'] . '次 ';
                    }
                }
                $v['gift_service_count'] = $gift_service_count_str;
            }
//            $member_card = Db::name('system_card')->field('id,sycs,title,sale_price,origin_price,single_pic,DATE_FORMAT(start_time,"%Y.%m.%d") start_time,DATE_FORMAT(end_time,"%Y.%m.%d") end_time,sale_number,count,gift_count,gift_service_count,type')->select()->toArray();
//            if (!empty($member_card)) {
//                foreach ($member_card as $k1 => &$v1) {
//                    $v1['sale_price'] = 0.01;
//                    $v1['origin_price'] = mt_rand(1, 1000);
//                    $v1['sycs'] = 0;
//                    $v1['t_type'] = 2;
//                    if ($v1['type'] == 1) {
//                        $v1['title'] = '(年卡)' . $v1['title'];
//                        $v1['single_pic'] = '/uploads/20190114/bef34313e30efae6c9da0eebfdbadc25.jpg';
//                    }
//                    if ($v1['type'] == 2) {
//                        $v1['title'] = '(次卡)' . $v1['title'];
//                        $v1['single_pic'] = '/uploads/20190114/46a645f2e7236ff6a70ff92507f92603.png';
//                    }
//                }
//            }
//            if (is_null($member_card)) $member_card = [];
//            $data = array_merge($data['data'], $member_card);
//            $page = new Page(count($data), 6, []);
//            $page_html = $page->show($data);// 分页显示输 __FUNCTION
        }

        return ['code' => 200, 'message' => '礼包列表', 'data' => $data];
    }

    public function detail($id,$member_id = 0){

        $data = Db::name('gift')
            ->field('sycs,id,title,describe,sale_price,origin_price,single_pic,multi_pic,DATE_FORMAT(start_time,"%Y.%m.%d") start_time,DATE_FORMAT(end_time,"%Y.%m.%d") end_time,sale_number,count')
            ->find($id);
        $data['multi_pic'] = unserialize($data['multi_pic']);
        $data1 = Db::name('config')->where('varname','in','web_service_tel,web_report_tel')->column('varvalue');

        if(!empty($member_id)){
            $auding_status['single_id'] = ['eq', $id];
            $auding_status['oe.member_id'] = ['eq', $member_id];
            $others_evaluate = function ($query) use ($id) {
                $query->where('auding_status', 'eq', '2')->where('single_id', 'eq', $id);
            };
            $evaluate_count = Db::name('order_evaluate oe')
                ->where($auding_status)->whereOr($others_evaluate)
                ->count('id');
            $evaluate_list = Db::name('order_evaluate oe')
                ->join('member m','oe.member_id = m.id','left')
                ->where($auding_status)->whereOr($others_evaluate)
                ->where('order_type',3)
                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')
                ->order('oe.id','desc')
                ->paginate(6)->toArray();
        }else{
            $evaluate_count = Db::name('order_evaluate oe')
                ->where(['auding_status' => 2,'single_id' => $id])
                ->count('id');
            $evaluate_list = Db::name('order_evaluate oe')
                ->order('oe.id','desc')
                ->join('member m','oe.member_id = m.id','left')
                ->where(['auding_status' => 2,'single_id' => $id])
                ->where('order_type',3)
                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')
                ->paginate(6)->toArray();
        }

        foreach ($evaluate_list['data'] as $k => &$v){
            $v['username'] = hideStr($v['username'],3,4);
        }

        return ['code' => 200,'message' => '礼包详情','data' => $data,'phone1' => $data1[0],'phone2' => $data1[0],'evaluate_count' => $evaluate_count,'evaluate_list' => $evaluate_list];
    }

    public function view($id,$field)
    {
        $data = Db::name('gift')->where('id',$id)->value($field);
        return response($data);
    }

    /**
     * @param $id
     * @param int $member_id
     * @param $order_type
     * @return array服务评价和礼包评价
     */

    public function evaluate_index($id, $member_id = 0,$order_type)
    {
        $auding_status['auding_status'] = ['eq', 2];
        $others_evaluate = [];
        $w = '';
        if (!empty($member_id)) {
            $auding_status['single_id'] = ['eq', $id];
            $auding_status['member_id'] = ['eq', $member_id];
            $others_evaluate = function ($query) use ($id) {
                $query->where('auding_status', 'eq', '2')->where('single_id', 'eq', $id);
            };
        }else{
            $evaluate_list = Db::name('order_evaluate oe')
                ->join('member m', 'oe.member_id = m.id')
                ->where('single_id', $id)
                ->where('order_type', $order_type)
                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')
                ->where($auding_status)
                ->whereOr($w)
                ->paginate(6)->toArray();
            foreach ($evaluate_list['data'] as $k => &$v) {
                $v['username'] = hideStr($v['username'], 3, 4);
            }
            $evaluate_count = Db::name('order_evaluate')->where('single_id', $id)->where('order_type', $order_type)->where($auding_status)
                ->whereOr($w)->count('id');
        }

        $order_name = '未知';
        if($order_type == 2){
            $order_name = '服务';
        }
        if($order_type == 3){
            $order_name = '礼包';
        }
        return ['code' => 200, 'message' => $order_name.'评价列表', 'evaluate_count' => $evaluate_count, 'evaluate_list' => $evaluate_list];
    }

    /**
     * 系统会员卡列表
     */
    public function system_card_list()
    {
        $data = Db::name('system_card')->field('id,title,single_pic,type,count,start_time,end_time')->select();
        return ['code' => 200 , 'message' => '系统会员卡列表', 'data' => $data];
    }
}